package com.cherry.networksdk

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.cherry.networksdk.response.DataResponse
import com.cherry.sdk.http.ApiService
import com.cherry.sdk.http.NetworkConfig
import com.cherry.sdk.http.NetworkSDK
import com.cherry.sdk.http.converter.ValueEnumAdapterFactory
import com.cherry.sdk.http.response.ResponseConfig
import com.cherry.sdk.http.token.TokenConfig
import com.cherry.sdk.http.token.TokenProvider
import com.parkingwang.okhttp3.LogInterceptor.LogInterceptor
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun get(view: View) {

    }

    fun post(view: View) {

        val tokenProvider = object : TokenProvider {
            override fun getToken(): String = ""
        }
        val headerMap = mapOf("User-Agent" to "ParkingWangAPIClientAndroid",
                "Accept-Version" to "V5.3.0")
        val networkConfig = NetworkConfig("http://116.62.132.145:9090/v5/", requestHeaderMap = headerMap,
                interceptors = listOf(LogInterceptor()), tokenConfig = TokenConfig(tokenProvider = tokenProvider))

        //添加枚举转换器
        NetworkSDK.init(networkConfig, ResponseConfig(typeAdapterFactoryList = listOf(ValueEnumAdapterFactory())))

        ApiService.create(TestService::class.java)
                .login(LoginParams().mallId("2")
                        .username("test01")
                        .password(HashEncryption.encrypt("123456", "SHA-256")))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : RxObserver<DataResponse<LoginModel>>() {
                    override fun onSuccess(data: Any?) {
                        content.text = data.toString()
                    }
                })


    }
}
